package com.hjh.usb.accessory

import android.annotation.SuppressLint
import android.hardware.usb.UsbAccessory
import androidx.lifecycle.ViewModel
import com.hjh.usb.accessory.communication.ConnectionState
import com.hjh.usb.accessory.communication.UsbFlow

class UsbConnectionViewModel : ViewModel() {

    @SuppressLint("StaticFieldLeak")
    var aoaService: AOAService? = null
    var serviceBound = false
    private var currentAccessory: UsbAccessory? = null

    fun setService(service: AOAService) {
        serviceBound = true
        aoaService = service
    }
    fun releaseService() {
        aoaService = null
        serviceBound = false
    }

    fun connectAccessory(accessory: UsbAccessory) {
        currentAccessory = accessory
        aoaService?.connectAccessory(accessory)
    }

    fun disconnect() {
        aoaService?.disconnect()
        currentAccessory = null
    }

    fun updateConnectionState(state: ConnectionState) {
        UsbFlow.updateConnectionState(state)
    }

    fun getCurrentAccessory(): UsbAccessory? = currentAccessory
}
